package org.jeecg.config.app.interceptor;

import cn.hutool.core.util.StrUtil;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.CommonAPI;
import org.jeecg.common.constant.CommonConstant;
import org.jeecg.common.exception.JeecgBoot401Exception;
import org.jeecg.common.exception.JeecgBootException;
import org.jeecg.common.system.util.JwtUtil;
import org.jeecg.common.system.vo.MemberModel;
import org.jeecg.common.util.RedisUtil;
import org.jeecg.config.app.AppContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Enumeration;

/**
 * @Description: 微信业务拦截
 * @author: Young Jun
 * @date: 2023年06月17日 16:48
 */
@Component
@Slf4j
public class WechatInterceptor implements HandlerInterceptor {

    @Autowired
    private CommonAPI commonAPI;
    @Autowired
    private RedisUtil redisUtil;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

        if (!(handler instanceof  HandlerMethod)) {
            //handler 可能是 RequestResourceHandler springboot 程序访静态资源 默认classpath下的static目录去查询
            return true;
        }

        Enumeration<String>headers= request.getHeaderNames();
        while (headers.hasMoreElements()){
            String key=headers.nextElement();
           String value= request.getHeader(key);
            log.info("request.getHeader:key:{}={}",key, value);
        }
        String uri = request.getRequestURI();
        log.info("request = >uri:{}", uri);
        String appToken = request.getHeader(CommonConstant.APP_TOKEN);
        log.info("appToken = >:{}", appToken);
        if (StrUtil.isEmpty(appToken)) {
            throw new JeecgBoot401Exception("未登录");
        }

        String openid = JwtUtil.getUsername(appToken);
        //检查是否过期
        Object o = redisUtil.get(openid);
        if (o == null) {
            throw new JeecgBoot401Exception("登录失败，token过期");
        }

        //检查token
        MemberModel memberModel = commonAPI.getMemberByOpenId(openid);
        if (memberModel == null) {
            throw new JeecgBootException("登录失败，找不到该账号");
        }

        AppContext.put(memberModel);
        return true;
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        //避免内存泄露
        AppContext.clear();
    }

    public static void getHeders(HttpServletRequest request){
        //2.获得所有头的名称
        Enumeration<String> headerNames = request.getHeaderNames();
        while(headerNames.hasMoreElements()) {//判断是否还有下一个元素
            String nextElement = headerNames.nextElement();//获取headerNames集合中的请求头
            String header2 = request.getHeader(nextElement);//通过请求头得到请求内容
            log.info("请求头=========={}" + nextElement + "VALUE:" + header2);
            //System.out.println(nextElement+":"+header2);
        }
    }
}
